package mse.tx.arch.pipes;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Properties;

import mse.FilterFramework;

/******************************************************************************************************************
* File:SinkFilterTemplate.java
* Course: 17655
* Project: Assignment 1
* Copyright: Copyright (c) 2003 Carnegie Mellon University
* Versions:
*	1.0 November 2008 - Initial rewrite of original assignment 1 (ajl).
*
* Description:
*
*
* Parameters: 		None
*
* Internal Methods:
*
*	public void run() - this method must be overridden by this class.
*
******************************************************************************************************************/

public class MultiInputDataWriterSinkFilter extends AbstractArrayMultiPortFilter implements IConfigurable
{
	protected File outFile = null;
	
	public void run()
    {
		// Write file
		PrintWriter dataOut = null; 
		try {
			dataOut = new PrintWriter(new BufferedWriter(
			        new FileWriter(outFile)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		byte databyte = 0;

/*************************************************************
*	This is the main processing loop for the filter. Since this
*   is a sink filter, we read until there is no more data
* 	available on the input port.
**************************************************************/

		// For testing purpose
		DataModel dataModel = null;
		byte[] dataBuffer = new byte[DataModel.BINARY_STREAM_SIZE];
		int recordByteCtr = 0;
		
		SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy:MM:dd:hh:mm:ss");
		
		while (true)
		{
			try
			{
				databyte = super.readFilterInputPort(0);

				dataBuffer[recordByteCtr] = databyte;
				recordByteCtr++;
				
				if (dataBuffer.length == recordByteCtr) {
					// this may affect performance
					recordByteCtr = 0;
					dataModel = DataModel.createFromBinary(dataBuffer);
					dataBuffer = new byte[DataModel.BINARY_STREAM_SIZE];
					
					// print to console
					System.out.printf("\n%s\t%9.5f\t%11.5f\t%11.5f"
							, timeStampFormat.format(dataModel.getTimestampDt())
							, dataModel.getTemperature()
							, dataModel.getAltitude()
							, dataModel.getPressure());
					dataOut.printf("\n%s\t%9.5f\t%11.5f\t%11.5f"
							, timeStampFormat.format(dataModel.getTimestampDt())
							, dataModel.getTemperature()
							, dataModel.getAltitude()
							, dataModel.getPressure());
				}
			} // try

			
/***************************************************************
*	When we reach the end of the input stream, an exception is
* 	thrown which is shown below. At this point, you should
* 	finish up any processing, close your ports and exit.
***************************************************************/

			catch (EndOfStreamException e)
			{
				
				// [ysahn] Close all the resources.
				dataOut.close();

				ClosePorts();
				
				break;

			} // catch

		} // while

   } // run

	@Override
	public void setConfiguration(Properties props)
	{
		if (props != null){
			setFilename(props.getProperty("filename"));
		}
	}
	
	public void setFilename(String filename)
	{
		if (filename != null)
			this.outFile = new File(filename);
	}

} // FilterTemplate